stages:
  - tests

### Базовая настройка Unit тестирования
.baseUnitTests: &baseUnitTests
  stage: tests
  image: golang:1.18.0-alpine3.15
  before_script:
    - apk update && apk add --no-cache git gcc libc-dev wget
  script:
    - go version
    - go get -v
    - go test ./... -v --short >> ../test-results.txt
    - cat ../test-results.txt | grep FAIL >> ../unit-tests-fails.txt || true
  artifacts:
    when: always
    name: Unit tests report
    paths:
      - ./test-results.txt
      - ./unit-tests-fails.txt
    expire_in: 2 weeks

### Базовая настройка тестирования с покрытием кода
.baseCoverageTests: &baseCoverageTests
  stage: tests
  image: golang:1.18.0-alpine3.15
  before_script:
    - apk update && apk add --no-cache git gcc libc-dev wget
  script:
    - go version
    - go get -v
    - go get github.com/boumenot/gocover-cobertura
    - go test -coverprofile=coverage.txt -covermode count ./...
    - go build github.com/boumenot/gocover-cobertura
    - ./gocover-cobertura < coverage.txt > coverage.xml
    - go tool cover -func=coverage.txt
  coverage: "/\\(statements\\)\\s+\\d+.?\\d+%/"
  artifacts:
    expire_in: 2 weeks
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

### Unit тестирование для ветки
UnitTests:
  <<: *baseUnitTests
  allow_failure: true
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_COMMIT_BRANCH

### Unit тестирование для тега
UnitTests_Final:
  <<: *baseUnitTests
  allow_failure: false
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH
      when: never

### Тестирование с покрытием кода для ветки
CoverageTests:
  <<: *baseCoverageTests
  allow_failure: true
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_COMMIT_BRANCH == "master"

### Тестирование с покрытием кода для тега
CoverageTests_Final:
  <<: *baseCoverageTests
  allow_failure: false
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH
      when: never
